#!/usr/bin/perl
# Count number of messaging events per day

use LogCount; # subroutines for time-series handling

$linecount = 5000;
$usage = "Usage: $0 [-lines count]\n";

@fields = qw(total newperson updateperson deleteperson newmachine);

# translation of monthnames (correct ones)
%months = (
"Jan" => 1,
"Feb" => 2,
"Mar" => 3,
"Apr" => 4,
"May" => 5,
"Jun" => 6,
"Jul" => 7,
"Aug" => 8,
"Sep" => 9,
"Oct" => 10,
"Nov" => 11,
"Dec" => 12,
	   );


while ($ARGV[0] =~ /^-./) {
    $opt = shift;
    if ($opt eq "-lines") {
	$linecount = shift;
    } elsif ($opt eq "-days") {
	$daycount = shift;
    } elsif ($opt eq "-byhour") {
	$byhour = 1;
    } elsif ($opt eq "-to") {
	$todate = shift;
    } else {
	die $usage;
    }
}
if ($byhour && $daycount) {
    $daycount *= 24; # 1 day = 24 hours; external beauty, not internal...
}
$thisyear = (localtime)[5] + 1900;
warn "This year is $thisyear\n";
unshift(@ARGV, "tail -$linecount logs/eventlog|") if !$ARGV[0];
while (<>) {
    if (/^\S\S\S (\S\S\S)\s+(\d+) (\d+):\d+:.* (\d\d\d\d)\D/) {
	$month = $months{$1};
	$date = sprintf "%4d-%02d-%02d", $4, $month, $2;
	# Sanity check....
	if ($4 > $thisyear) {
	    warn "Future year, skipped: $_";
	    undef $date;
	    next;
	}
	if ($byhour) {
	    $hour = $3;
	    # correct for GMT....
	    if (/ GMT /) {
		$hour += 1; # WRONG WRONG WRONG....summertime!!!!!
	    }
	    $date .= sprintf "T%02d", $hour;
	} else {
	    $date .= "T00";
	}
	if ($todate && $date ge $todate) {
	    ++ $futurelines;
	    undef $date;
	    next;
	}
	++ $count{$date}{total};
	if ($count{$date}{total} == 1) {
	    $seqno{$date} = $seqno;
	    ++ $seqno;
	}
    } else {
	next if !$date;
	#--------- MACHINE ------
	if (/^Machine registered/) {
	    # ignore - counted below
	} elsif (/^Creating machine/) {
	    ++ $count{$date}{newmachine};
	} elsif (/userid is \d+ \(old\)/) {
	    # ignore - written on entermachines
	} elsif (/^Updating machine/) {
	    ++ $count{$date}{updatemachine};
	    #--------- NEW PERSON -----
	} elsif (/userid is \d+ \(new\)/) {
	    ++ $count{$date}{newperson};
	} elsif (/^(Vote recorded|Message sent back|Reply for|Person registe)/) {
	    # Ignore - counted above
	    #--------- PERSON CHANGE -----
	} elsif (/^Person \d+ updated/) {
	    ++ $count{$date}{updateperson};
	} elsif (/User \d+ email changed to/) { 
	    # ignore - counted above
	    #--------- PERSON DELETE ----
	} elsif (/Person \d+ deleted/) {
	    ++ $count{$date}{deleteperson};
	    #--------- REPORTS ----
	} elsif (/Report \S+ sent/) {
	    ++ $count{$date}{reports}
	    #-------- MISC STUFF ----
	} elsif (/^Usergroup registered/) {
	    ++ $count{$date}{newusergroup};
	} elsif (/^Creating usergroup/) {
	    # ignore - counted above
	} elsif (/^Updating usergroup/) {
	    ++ $count{$date}{changeusergroup};
	} elsif (/^\d+ friends registered/) {
	    ++ $count{$date}{newfriend};
	} elsif (/^Message ignored: No "Linux" in subject/) {
	    ++ $count{$date}{bademail};
	} elsif (/^(Help sent|Vote ignored)/) {
	    # Ignore
	} elsif (/^(>>>>|operator message|Ignoring illegal)/) {
	    # warning messages - ignore
	} elsif (/^\s*$/) {
	    # blank lines
	} elsif (/^(: Message from|Failed fetch|ld.so.1:|Host not found)/) {
	    # bugs
	} else {
	    warn "Unparsed: $_\n";
	}
    }
}

warn "$futurelines lines of data after $todate skipped\n"
    if $futurelines;

printf "#%10s: ", "Date";
for $field (@fields) {
    printf " %5.5s", $field;
}
print "\n";

LogCount::zerofilltimes(\%count, $byhour);

@keys = sort(keys(%count));
$nkeys = $#keys;
if ($daycount) {
    $skip = $nkeys - $daycount;
    warn "Skipping $skip of $nkeys output lines\n";
}


for $date (sort keys(%count)) {
    warn "Last date not printed is $date\n" if $skip == 0;
    next if ($skip-- >= 0);
    next if $date lt "1990"; # Delete some rubbish dates
    last if $todate && $date ge $todate;
    printf "%10s ", $date;
    for $field (@fields) {
	printf " %5d", $count{$date}{$field};
    }
    print "\n";
}
warn "First date not printed was $date\n";









